package edu.scu.lijobs.android;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.google.code.linkedinapi.client.JobsApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.enumeration.CompanyField;
import com.google.code.linkedinapi.client.enumeration.JobField;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.google.code.linkedinapi.schema.Company;
import com.google.code.linkedinapi.schema.JobBookmark;
import com.google.code.linkedinapi.schema.JobBookmarks;

public class LinkedInApiManager {
	
	
	public static final String CONSUMER_KEY = "45vjs1mpz5il";
	public static final String CONSUMER_SECRET = "03yYik0uZ7lHkWfS";
	public static LinkedInAccessToken LINKED_IN_ACCESS_TOKEN = null;
	private JobsApiClient jobsClient = null;
	private LinkedInApiClient client = null;
	private static LinkedInApiManager INSTANCE;
    private LinkedInApiClientFactory factory   = LinkedInApiClientFactory.newInstance(CONSUMER_KEY, CONSUMER_SECRET);
	private static Map<String,Company> companyMap;
	
	
	private ExecutorService executor;
	
	
	public static LinkedInApiManager getInstance() {
		if(INSTANCE == null) {
			INSTANCE = new LinkedInApiManager();
		}
		return INSTANCE;
		
		
	}
	
	
	
	
	
	public LinkedInApiManager() {
		jobsClient = factory.createLinkedInApiClient(LINKED_IN_ACCESS_TOKEN);
		client = factory.createLinkedInApiClient(LINKED_IN_ACCESS_TOKEN);
		companyMap = new HashMap<String,Company>();
		executor = Executors.newFixedThreadPool(Integer.MAX_VALUE);
	}
	
	
	
	public  JobsApiClient getJobsApiClient() {
		return jobsClient;
	}
	
	
	public LinkedInApiClient getLinkedInApiClient() {
		return client;
	}
	
	
	
	
	
	
	public Company getCompany(final String companyId) {
		
		
		
		
				
				
				
				

		
	  
	 
	try {
		
		Company company = companyMap.get(companyId);
		if(company == null) {
			
			FutureTask<Company> task = new FutureTask<Company>(new Callable<Company>() {
				
				@Override
				public Company call() {
					Company company = client.getCompanyById(companyId, EnumSet.allOf(CompanyField.class));
					return company;
					
				}
			});
			Thread thread = new Thread(task) ;
			thread.start();
			company = task.get();
			companyMap.put(companyId, company);
			
			
			
		}
		
		return company;
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
	  
	}
		
		
		
		
		
		
	
	
	
	
	


}
